﻿--[[
   @brief    view for wegame mainframe
   @author   hqzheng
   @version  1.0
   @date     2017-04-12
--]]

wegame_mainframe_view =
{
    dialog_id = Tenio.INVALID_CONTROLID;
    first_page_list_panel_id = Tenio.INVALID_CONTROLID;
    client_area_id = Tenio.INVALID_CONTROLID;
    dialog_ptr = nil;
    first_page_list_panel_ptr = nil;
    max_btn_id = Tenio.INVALID_CONTROLID;
    title_id = Tenio.INVALID_CONTROLID;
    title_ani_id = Tenio.INVALID_CONTROLID;
    title_operation_id = Tenio.INVALID_CONTROLID;
    app_pnl_id = Tenio.INVALID_CONTROLID;
    app_btn_id = Tenio.INVALID_CONTROLID;

    e_framestate = {normal=1,min=2,max=3};
    is_closed = false;
    is_reset_qb_wnd = false;
    framestate = 1;

    has_app_login = nil;
    app_tips_data = nil;

    MENU_OFFSET_X = 76;
    MENU_OFFSET_Y = 22;
    BTN_APPTIPS_WIDTH = 100;
    BANNER_LIST_WIDTH = 190;

    RESIZE_ANI_TYPE = {
        ANI_CLOSE = 0,
        ANI_OPEN  = 1,
    };

    FULLSCREEN_MODE_TYPE = {
        NORMAL_MODE = 0,
        FULLSCREEN_MODE = 1,
    };

    is_list_screen_mode_visible = false;
    is_packup_btn_screen_mode_visible = false;
    pack_up_btn_ptr = nil;

    need_save_pos = false;

    -- 标识设置按钮红点是否出现
    setting_red_dot_key = "app.red_dot.setting_btn_for_color_mode";

    init = function ()
        -- ierd_tgp.tpf_ui.Require("wegame_mainframe\\navigation_tab\\navigation_tab.lua");
        -- ierd_tgp.tpf_ui.Require("wegame_mainframe\\tools_panel.lua");
        ierd_tgp.tpf_ui.Require("wegame_mainframe\\web_operation.lua");
        ierd_tgp.tpf_ui.Require("wegame_mainframe\\app_tips.lua");

        comm_center.reg_service("i_service_is_mainframe_iconic",
                                "wegame_mainframe_view.query_is_mainframe_iconic");
        wegame_mainframe_view.max_btn_id = ui_utility.get_ctrl_id_by_path(
            wegame_mainframe_view.dialog_id, "pnlTitle\\pnlSysBtn\\btnMax");
        wegame_mainframe_view.client_area_id = ui_utility.get_ctrl_id_by_path(
            wegame_mainframe_view.dialog_id, "pnlClient");


        if base_utility.CLIENT_TYPE.CHINA_VER == base_utility.get_client_version_type() then
            wegame_mainframe_view.app_pnl_id = ui_utility.get_ctrl_id_by_path(
                        wegame_mainframe_view.dialog_id, "pnlTitle\\pnlSysBtn\\pnlAPP");
            wegame_mainframe_view.app_btn_id = ui_utility.get_ctrl_id_by_path(
                        wegame_mainframe_view.app_pnl_id, "btnAPP");
            if Tenio.INVALID_CONTROLID ~= wegame_mainframe_view.app_btn_id then
                ui_utility.setproperty_by_id(wegame_mainframe_view.app_btn_id, "OnClick",
                    "wegame_mainframe_view.on_click_app_btn");
                ui_utility.setproperty_by_id(wegame_mainframe_view.app_btn_id, "OnMouseHover",
                    "wegame_mainframe_view.on_enter_app_btn");
                ui_utility.setproperty_by_id(wegame_mainframe_view.app_btn_id, "OnMouseLeave",
                    "wegame_mainframe_view.on_leave_app_btn");
            ui_utility.setproperty_by_id(wegame_mainframe_view.app_btn_id, "OnKillFocus",
                "wegame_mainframe_view.on_leave_app_btn");
                app_tips.init(wegame_mainframe_view.dialog_id);
            end
        end

        -- 下拉按钮红点
        local config_btn_cid = ui_utility.get_ctrl_id_by_path(wegame_mainframe_view.dialog_id,
                            "pnlTitle\\pnlSysBtn\\btnConfig");
        RedDotHelper.AddRedDot(config_btn_cid, main_menue_view.setting_red_dot_key);

        -- 导航tab
        local nav_tab_panel_id = ui_utility.get_ctrl_id_by_path(
            wegame_mainframe_view.dialog_id, "pnlTitle\\pnlTabs");
        wegame_mainframe_view.first_page_list_panel_id = ui_utility.get_ctrl_id_by_path(
            wegame_mainframe_view.dialog_id, "DlgList");
        wegame_mainframe_view.first_page_list_panel_ptr = ui_utility.get_ctrl_ptr_by_id(
            wegame_mainframe_view.first_page_list_panel_id);
        local svr_nav_data = {};
        svr_nav_data["nav_tab_panel_id"] = nav_tab_panel_id;
        svr_nav_data["dialog_id"] = wegame_mainframe_view.dialog_id;
        comm_center.call_service("i_svr_show_navigation_tab", svr_nav_data);

        if nil ~= wegame_mainframe_view.first_page_list_panel_ptr  then
            wegame_mainframe_view.first_page_list_panel_ptr:SetParent(
                wegame_mainframe_view.dialog_ptr);
            -- wegame_mainframe_view.move_list();
        end

        local btn_ptr = ui_utility.get_ctrl_ptr_by_path(
            wegame_mainframe_view.dialog_id, "PackUpCtn");
        wegame_mainframe_view.pack_up_btn_ptr = btn_ptr;
        if nil ~= bnt_ptr then
            btn_ptr:SetParent(wegame_mainframe_view.dialog_ptr);
        end

        -- 工具栏
        local tools_panel_id = ui_utility.get_ctrl_id_by_path(
            wegame_mainframe_view.dialog_id, "pnlTitle\\pnlTools");
        local svr_tools_data = {};
        svr_tools_data["tools_panel_id"] = tools_panel_id;
        svr_tools_data["dialog_id"] = wegame_mainframe_view.dialog_id;
        comm_center.call_service("i_svr_show_tools_panel", svr_tools_data);
        -- tools_panel.init(tools_panel_id, wegame_mainframe_view.dialog_id);
        -- tools_panel.show(true);

        -- 浏览器导航
        local web_operation_panel_id = ui_utility.get_ctrl_id_by_path(
            wegame_mainframe_view.dialog_id, "pnlTitle\\pnlNav");
        web_operation.init(web_operation_panel_id);
        web_operation.show(true);

        wegame_mainframe_view.title_id = ui_utility.get_ctrl_id_by_path(
            wegame_mainframe_view.dialog_id, "pnlTitle");
        wegame_mainframe_view.title_ani_id = ui_utility.get_ctrl_id_by_path(
            wegame_mainframe_view.dialog_id, "pnlTitle\\pnlTitleANI");
        wegame_mainframe_view.title_operation_id = ui_utility.get_ctrl_id_by_path(
            wegame_mainframe_view.dialog_id, "pnlTitle\\pnlTitleOperation");
        wegame_mainframe_view.drop_id = ui_utility.get_ctrl_id_by_path(
            wegame_mainframe_view.dialog_id, "pnlSelected\\tabSelected");

        wegame_mainframe_view.copyright_id = ui_utility.get_ctrl_id_by_path(
            wegame_mainframe_view.dialog_id, "pnlTitle\\stcCopyright");


        wegame_mainframe_view.change_logo_by_region();

        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            ui_utility.setproperty_by_id(wegame_mainframe_view.copyright_id, "Visible", "true");
        end

    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[wegame_mainframe_view] %s", msg), core_msg);
    end;

    change_logo_by_region = function ()
      --  local user_region = "";
      --  local region_tbl = comm_center.call_service("Svr_GetUserSelfRegion", {});
      --  if region_tbl ~= nil and region_tbl.user_region ~= nil then
      --      user_region = region_tbl.user_region;
      --  end;

        local logo = ui_utility.get_ctrl_id_by_path(wegame_mainframe_view.dialog_id, "pnlTitle\\pnlTitleBG");
        --if user_region == "CN" then
            ui_utility.setproperty_by_id(logo,
                "Background", "ONEIMAGE:client_mainframe\\top_title\\logo.png;Stretch;0");
       -- else
       --     ui_utility.setproperty_by_id(logo,
        --        "Background", "ONEIMAGE:client_mainframe\\top_title\\logo_overseas.png;Center;0");
       -- end
    end;

    web_notify_to_set_background = function (pic_image, color, image_position)
        -- 调用接口进行页面向左移动动画
        local callback_func = web_register_center.get_func_by_browser(
            "web_mainframe_comm_listen", CID_MAINFRAME_QBWEB);
        if callback_func ~= nil and callback_func ~= "" then
            local move_info = {};
            move_info["background_image"] = pic_image;
            move_info["background_color"] = color;
            move_info["image_position"] = image_position;
            local rsp_data_table = 
                web_utility.notify_event_comm_data("web_mainframe_comm_listen");
            rsp_data_table.data.data = move_info;
            rsp_data_table.data.custom_cmd = "set_web_mainframe_bg";
            web_utility.lua_call_js_ex(callback_func, 
                base_utility.table_to_json(rsp_data_table), CID_MAINFRAME_QBWEB);
        end
    end;

    web_notify_to_init = function (move_type)
        -- 调用接口进行页面向左移动动画
        local callback_func = web_register_center.get_func_by_browser(
            "web_mainframe_comm_listen", CID_MAINFRAME_QBWEB);
        if callback_func ~= nil and callback_func ~= "" then
            local move_info = {};
            move_info["move_type"] = move_type;
            local rsp_data_table = 
                web_utility.notify_event_comm_data("web_mainframe_comm_listen");
            rsp_data_table.data.data = move_info;
            rsp_data_table.data.custom_cmd = "web_mainframe_start_load_page";
            web_utility.lua_call_js_ex(callback_func, 
                base_utility.table_to_json(rsp_data_table), CID_MAINFRAME_QBWEB);
        end
    end;

    show = function(first_show)
        wegame_mainframe_view.inner_log(string.format("first_show = %s", tostring(first_show)));
        if wegame_mainframe_model.is_blacklist_user then
            return;
        end

        if base_utility.is_big_picture_ui() then
            comm_center.call_service("i_service_show_bp_wnd", {show=1});
            return false;
        end

        local just_loaded = false;
        if Tenio.INVALID_CONTROLID == wegame_mainframe_view.dialog_id then
            wegame_mainframe_view.dialog_id = ui_utility.get_ctrl_id_by_cid(
                CID_MAINFRAME_Dialog_MainFrame);
            if Tenio.INVALID_CONTROLID == wegame_mainframe_view.dialog_id then
                wegame_mainframe_view.dialog_id = layout_mgr.restore_layout("wegame_mainframe\\wegame_mainframe.tpf");
            end
            wegame_mainframe_view.dialog_ptr = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.dialog_id);
            wegame_mainframe_view.init();
            wegame_mainframe_view.set_list_panel_visible(false);
            -- 通知显示新手引导
            event_center.send_event(user_guide_event.SHOW_MAINFRAME_USER_GUIDE, "");
            just_loaded = true;
        end

        -- -- 通知显示主界面操作引导
        -- if true == first_show and true == just_loaded then
        --     event_center.send_event(user_guide_event.SHOW_MAINFRAME_USER_GUIDE, "");
        -- end

        local component = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.dialog_id);
        
        if nil == component then
            return false;
        end

        -- 解决俩次navigate的问题 -- begin
        -- if  wegame_mainframe_control.has_already_navigate == false then
            wegame_mainframe_view.load_web_component();
        -- end

        if wegame_mainframe_control.notify_web_init_when_show_ == true then
            wegame_mainframe_control.notify_web_init_when_show_ = false; 
            wegame_mainframe_view.web_notify_to_init();
            wegame_mainframe_view.set_browser_working_set_size(0, 0);
        end;


        -- wegame_mainframe_view.resume_list_panel();

        if first_show == false then
            if wegame_mainframe_view.framestate == wegame_mainframe_view.e_framestate.normal then
                if not just_loaded and not wegame_mainframe_view.is_main_wnd_full_screen() then
                    wegame_mainframe_view.inner_log("[show]not just loaded and restore");
                    component:Restore();
                end    
            elseif wegame_mainframe_view.framestate == wegame_mainframe_view.e_framestate.min then
                component:Restore();
            end
            if true == wegame_mainframe_model.unvisible_display_change then
                 wegame_mainframe_view.adjust_mainframe_size(false);
                 wegame_mainframe_model.unvisible_display_change = false;
            end

            if true == wegame_mainframe_view.is_main_wnd_postion_or_size_abnormal() then
                wegame_mainframe_view.adjust_mainframe_size(false);
            end

            component:BringToTop();
            component:Show(TRUE);    
        else
            if true == just_loaded then
                if nil ~= wegame_mainframe_model.save_size and 
                    true == wegame_mainframe_model.save_size.is_max then
                    wegame_mainframe_view.max_btn_click(nil, wegame_mainframe_view.max_btn_id);
                    base_utility.mainframe_report("main_window", "launch_max_mode");
                else
                    wegame_mainframe_view.adjust_mainframe_size(true);
                end
                wegame_mainframe_view.inner_log("[show]first show and just load, move to last pos");
            end

            if wegame_mainframe_view.framestate ~= wegame_mainframe_view.e_framestate.max then
                component:Restore();
            end

            if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() and 
               false == wegame_mainframe_model.has_size_record then
                wegame_mainframe_view.max_btn_click(nil, wegame_mainframe_view.max_btn_id);
                wegame_mainframe_view.framestate = wegame_mainframe_view.e_framestate.max;
            elseif true ~= just_loaded then
                wegame_mainframe_view.framestate = wegame_mainframe_view.e_framestate.normal;
            end

            component:BringToTop();
            component:MoveToCenter();
            component:Show(TRUE);
            wegame_mainframe_model.screan_width, wegame_mainframe_model.screan_height = 
                ui_utility.get_screen_resolution();
        end

        if component:IsVisible() ~= TRUE then
            component:Restore();
            component:Show(TRUE); 
        end
        if not wegame_mainframe_view.is_main_wnd_in_screen() then
            component:MoveToCenter(); 
            wegame_mainframe_view.inner_log("[show] main wnd not in srceen, move to center");  
        end

        -- base_utility.mainframe_report("main_window", "main_wnd_show");
        
        return true;
    end;

    -- 调整主窗口尺寸
    adjust_mainframe_size = function (first_show)
        local suitable_height = wegame_mainframe_model.last_wnd_size.height;
        local suitable_width = wegame_mainframe_model.last_wnd_size.width;
        if nil ~= wegame_mainframe_model.save_size then
            if nil ~= wegame_mainframe_model.save_size.height then 
                if wegame_mainframe_model.save_size.height > suitable_height or 
                    (wegame_mainframe_model.save_size.height >= wegame_mainframe_model.min_wnd_height and 
                     wegame_mainframe_model.save_size.height < suitable_height) then
                    suitable_height = wegame_mainframe_model.save_size.height;
                elseif wegame_mainframe_model.save_size.height < wegame_mainframe_model.min_wnd_height then
                    suitable_height = wegame_mainframe_model.min_wnd_height;
                end
            end
            if nil ~= wegame_mainframe_model.save_size.width then
                 if wegame_mainframe_model.save_size.width  > suitable_width or 
                     (wegame_mainframe_model.save_size.width >= wegame_mainframe_model.min_wnd_width and
                      wegame_mainframe_model.save_size.width < suitable_width)then
                    suitable_width = wegame_mainframe_model.save_size.width;
                 elseif wegame_mainframe_model.save_size.width < wegame_mainframe_model.min_wnd_width then
                    suitable_width = wegame_mainframe_model.min_wnd_width;
                 end
            end
        end
        local cur_resolution_x = 0;
        local cur_resolution_y = 0;
        local cur_work_area_x = 0;
        local cur_work_area_y = 0;
        cur_resolution_x, cur_resolution_y, cur_work_area_x, cur_work_area_y = ui_utility.get_screen_resolution();
        if suitable_height  > cur_work_area_y then
            suitable_height = (cur_work_area_y - 100);  -- 上下预留50像素
        end

        if suitable_width  > cur_work_area_x then
            suitable_width = cur_work_area_x / ui_utility.get_dpi_scale();
        end

        wegame_mainframe_view.inner_log(string.format("[adjust_mainframe_size]width:%s,height:%s",
            tostring(suitable_width), tostring(suitable_height)), true);
        wegame_mainframe_view.resize_wnd(suitable_width, suitable_height);

        if suitable_height < wegame_mainframe_model.min_wnd_height then
            ui_utility.setproperty_by_id(wegame_mainframe_view.dialog_id, "MinHeight", suitable_height);
        end

        if suitable_width < wegame_mainframe_model.min_wnd_width then
            ui_utility.setproperty_by_id(wegame_mainframe_view.dialog_id, "MinWidth", suitable_width);
        end

        if nil ~= wegame_mainframe_view.dialog_ptr then
            wegame_mainframe_view.dialog_ptr:BringToTop();
            wegame_mainframe_view.dialog_ptr:MoveToCenter();
        end
    end;

    -- 系统菜单按钮点击
    sys_btn_click = function (uilib, control_id)
        base_utility.mainframe_report("main_window", "mainframe_btn_click", "sysmenu");
        -- 显示菜单
        local menu_id = ui_utility.get_ctrl_id_by_path(wegame_mainframe_view.dialog_id, "mnShop");
        local btn_tab_id = ui_utility.get_ctrl_parent_id(control_id);
        local btn_tab_ptr = ui_utility.get_ctrl_ptr_by_id(btn_tab_id);
        if nil == btn_tab_ptr or nil == wegame_mainframe_view.dialog_ptr then
            return;
        end
        local btn_tab_rc = btn_tab_ptr:GetRectEx(TRUE,TRUE);
        local mainframe_rc =  wegame_mainframe_view.dialog_ptr:GetRectEx(TRUE,TRUE);
        local offset_x = btn_tab_rc.left - mainframe_rc.left + wegame_mainframe_view.MENU_OFFSET_X;
        local offset_y = btn_tab_rc.top - mainframe_rc.top + wegame_mainframe_view.MENU_OFFSET_Y;

        local info = {};
        info["menu_id"] = menu_id;
        info["is_show"] = true;
        info["offset_x"] = offset_x;
        info["offset_y"] = offset_y;
        local json_str = base_utility.table_to_json(info);
        event_center.send_event(main_menue_event.MAIN_MENUE_OPEN_MENUE, json_str);
    end;
    
    -- 最小化按钮点击
    min_btn_click = function (uilib, control_id)
        comm_center.call_service("i_svr_save_main_wnd_pos", {});
        base_utility.mainframe_report("main_window", "mainframe_btn_click", "min");
        local dialog_ptr = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.dialog_id);
        if nil == dialog_ptr then
            return;
        end
        dialog_ptr:Minimize();
        wegame_mainframe_view.framestate = wegame_mainframe_view.e_framestate.min;
    end;

    -- 最大化按钮点击
    max_btn_click = function (uilib, control_id)
       local dialog_ptr = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.dialog_id);
       if nil == dialog_ptr then
           return;
       end
       if nil ~= uilib then
           wegame_mainframe_view.need_save_pos = true;
       end;
       if wegame_mainframe_view.framestate == wegame_mainframe_view.e_framestate.normal then
            if nil ~= uilib then
                base_utility.mainframe_report("main_window", "mainframe_btn_click", "max");
            end
            dialog_ptr:Maximize();
            wegame_mainframe_view.framestate = wegame_mainframe_view.e_framestate.max;
            ui_utility.setproperty_by_id(control_id,
                "Background", "ONEIMAGE:client_mainframe\\top_title\\WM_Normal.png;Stretch;0");
            ui_utility.setproperty_by_id(control_id, 
                "BackgroundHover", "ONEIMAGE:client_mainframe\\top_title\\WM_Hover.png;Stretch;0");
            ui_utility.setproperty_by_id(control_id,
                "BackgroundPressed", "ONEIMAGE:client_mainframe\\top_title\\WM_Press.png;Stretch;0");
            ui_utility.setproperty_by_id(control_id,
                "BackgroundDisable", "ONEIMAGE:client_mainframe\\top_title\\WM_Disabled.png;Stretch;0");
            ui_utility.setproperty_by_id(wegame_mainframe_view.max_btn_id,
                "TextTip_LID", "ID_TPF_WINDOWED");
        elseif wegame_mainframe_view.framestate == wegame_mainframe_view.e_framestate.max then
            base_utility.mainframe_report("main_window", "mainframe_btn_click", "winmod");
            dialog_ptr:Restore();
            dialog_ptr:MoveToCenter();
            dialog_ptr:BringToTop();
            wegame_mainframe_view.framestate = wegame_mainframe_view.e_framestate.normal;
            ui_utility.setproperty_by_id(control_id,
                "Background", "ONEIMAGE:client_mainframe\\top_title\\Max_Normal.png;Stretch;0");
            ui_utility.setproperty_by_id(control_id, 
                "BackgroundHover", "ONEIMAGE:client_mainframe\\top_title\\Max_Hover.png;Stretch;0");
            ui_utility.setproperty_by_id(control_id,
                "BackgroundPressed", "ONEIMAGE:client_mainframe\\top_title\\Max_Press.png;Stretch;0");
            ui_utility.setproperty_by_id(control_id,
                "BackgroundDisable", "ONEIMAGE:client_mainframe\\top_title\\Max_Disabled.png;Stretch;0");
            ui_utility.setproperty_by_id(wegame_mainframe_view.max_btn_id,
                "TextTip_LID", "ID_TPF_MAXIMIZE");
            
        end
    end;

    -- 关闭按钮点击
    close_btn_click = function (uilib, control_id)
        wegame_mainframe_view.inner_log("close");
        comm_center.call_service("i_svr_save_main_wnd_pos", {});
        if wegame_mainframe_model.exit_when_close == 1 then
            event_center.send_event(main_menue_event.MAIN_MENUE_WILL_EXIT_TGP, "");
        else
            wegame_mainframe_view.close_main_window();
        end;

        tqos_mgr.remember_btn_clicked("MINIWINDOW_TGP_CLOSE_BTN");
    end;

    -- dailog 最小化的回调函数
    on_window_mini = function()
        wegame_mainframe_view.inner_log("[on_window_mini]");
        wegame_mainframe_view.framestate = wegame_mainframe_view.e_framestate.min;
        event_center.send_event(mainframe_event.MAIN_WND_MINIMIZE, "");
    end;

    -- dailog 关闭的回调函数
    on_window_closed = function()
        wegame_mainframe_view.is_closed = true;
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib ~= nil then
            game_lib:notify_main_wnd_closed();
        end;
        wegame_mainframe_view.unload_web_component();

        if wegame_mainframe_control.destroy_when_close_wnd_ then
            layout_mgr.auto_close_all();
        else
            wegame_mainframe_view.load_web_component();
            
            local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
            if tpfui ~= nil then
                tpfui:AddInterval('wegame_mainframe_view.set_browser_mem', 5000);
            end;

            wegame_mainframe_control.notify_web_init_when_show_ = true;
        end;

        comm_center.send_broadcast("i_broadcast_close_main_wnd", {});
    end;

    set_browser_mem = function()
        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            tpfui:RemoveInterval('wegame_mainframe_view.set_browser_mem');
        end;
        if wegame_mainframe_view.is_closed == true then
            wegame_mainframe_view.set_browser_working_set_size(20, 20);
        end;
    end;

    -- dailog 显示的回调函数
    on_show = function()
        wegame_mainframe_view.is_closed = false;
    end;

    -- 窗口restore事件
    on_window_restore = function ()
        wegame_mainframe_view.inner_log("[on_window_restore]");
        if true == wegame_mainframe_model.unvisible_display_change then
            wegame_mainframe_view.adjust_mainframe_size(false);
            wegame_mainframe_model.unvisible_display_change = false;
        end

        wegame_mainframe_view.framestate = wegame_mainframe_view.e_framestate.normal;

        ui_utility.setproperty_by_id(wegame_mainframe_view.max_btn_id,
            "Background", "ONEIMAGE:client_mainframe\\top_title\\Max_Normal.png;Stretch;0");
        ui_utility.setproperty_by_id(wegame_mainframe_view.max_btn_id, 
            "BackgroundHover", "ONEIMAGE:client_mainframe\\top_title\\Max_Hover.png;Stretch;0");
        ui_utility.setproperty_by_id(wegame_mainframe_view.max_btn_id,
            "BackgroundPressed", "ONEIMAGE:client_mainframe\\top_title\\Max_Press.png;Stretch;0");
        ui_utility.setproperty_by_id(wegame_mainframe_view.max_btn_id,
            "BackgroundDisable", "ONEIMAGE:client_mainframe\\top_title\\Max_Disabled.png;Stretch;0");
        ui_utility.setproperty_by_id(wegame_mainframe_view.max_btn_id,
            "TextTip_LID", "ID_TPF_MAXIMIZE");

        event_center.send_event(mainframe_event.MAIN_WND_RESTORE, "");
        base_utility.mainframe_report("main_window", "mainframe_action", "winmod_action");
        if wegame_mainframe_view.need_save_pos == true then
            wegame_mainframe_view.need_save_pos = false;
            comm_center.call_service("i_svr_save_main_wnd_pos", {});
        end;
    end;

    -- 窗口最大化事件
    on_window_maxi = function ()
        wegame_mainframe_view.inner_log("[on_window_maxi]", true);
        wegame_mainframe_view.framestate = wegame_mainframe_view.e_framestate.max;
        ui_utility.setproperty_by_id(wegame_mainframe_view.max_btn_id,
            "Background", "ONEIMAGE:client_mainframe\\top_title\\WM_Normal.png;Center;0");
        ui_utility.setproperty_by_id(wegame_mainframe_view.max_btn_id, 
            "BackgroundHover", "ONEIMAGE:client_mainframe\\top_title\\WM_Hover.png;Center;0");
        ui_utility.setproperty_by_id(wegame_mainframe_view.max_btn_id,
            "BackgroundPressed", "ONEIMAGE:client_mainframe\\top_title\\WM_Press.png;Center;0");
        ui_utility.setproperty_by_id(wegame_mainframe_view.max_btn_id,
            "BackgroundDisable", "ONEIMAGE:client_mainframe\\top_title\\WM_Disabled.png;Center;0");
        ui_utility.setproperty_by_id(wegame_mainframe_view.max_btn_id,
            "TextTip_LID", "ID_TPF_WINDOWED");
        base_utility.mainframe_report("main_window", "mainframe_action", "winmax_action");

        comm_center.call_service("i_svr_refresh_nav_tab_status", {});

        if wegame_mainframe_view.need_save_pos == true then
            wegame_mainframe_view.need_save_pos = false;
            comm_center.call_service("i_svr_save_main_wnd_pos", {});
        end;
    end;

    -- 窗口尺寸/位置改变
    on_wnd_poschange = function ()
        if nil == wegame_mainframe_view.dialog_ptr then
            return;
        end

        local rc = wegame_mainframe_view.dialog_ptr:GetRectEx(TRUE,TRUE);
        local cur_w = rc.right - rc.left;
        local cur_h = rc.bottom - rc.top;
        if wegame_mainframe_model.last_wnd_size.width ~= cur_w or 
            wegame_mainframe_model.last_wnd_size.height ~= cur_h or 
            wegame_mainframe_model.last_wnd_size.x ~= rc.left or 
            wegame_mainframe_model.last_wnd_size.y ~= rc.top then
            -- 主界面位置变更广播
            local info = {mainframe_dialog_id = wegame_mainframe_view.dialog_id}
            comm_center.send_broadcast("i_broadcast_mainframe_pos_change", info);
            -- wegame_mainframe_view.move_list();
        end

        if wegame_mainframe_model.last_wnd_size.width ~= cur_w or 
            wegame_mainframe_model.last_wnd_size.height ~= cur_h then
            -- 主界面大小变更广播
            local info = {mainframe_dialog_id = wegame_mainframe_view.dialog_id}
            comm_center.send_broadcast("i_broadcast_mainframe_size_change", info);
        end

        wegame_mainframe_model.last_wnd_size.x = rc.left;
        wegame_mainframe_model.last_wnd_size.y = rc.top; 
        wegame_mainframe_model.last_wnd_size.width = cur_w;
        wegame_mainframe_model.last_wnd_size.height = cur_h;

        if true == wegame_mainframe_view.is_pack_up_btn_visible() then
            wegame_mainframe_view.adjust_pack_up_btn_position();
        end
    end;

    -- 关闭主窗口（非退出）
    close_main_window = function()
        local component = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.dialog_id);
        if component ~= nil and component:IsVisible() ~= FALSE then
            component:Show(FALSE);
            wegame_mainframe_view.on_window_closed();
        end;
    end;

    -- 打开url
    navigate_url = function(title, url)
        if url == nil then
            wegame_mainframe_view.inner_log("navigateUrl：url is nil");
            return false;
        end

        wegame_mainframe_view.inner_log("navigateUrl："..url);
        wegame_mainframe_view.current_url = url;
        local webctn_ptr = ui_utility.get_ctrl_ptr_by_path(wegame_mainframe_view.dialog_id,
            "pnlClient\\Ctn_WebArea\\QBWebBrowser");

        if webctn_ptr == nil then
            wegame_mainframe_view.inner_log("navigateUrl, webctn_ptr is nil");
            return false;
        end

        local qbweb_ptr = tolua.cast(webctn_ptr, "Tenio::IQBWebBrowser");
        if qbweb_ptr == nil then
            wegame_mainframe_view.inner_log("navigateUrl, qbweb_ptr is nil");
            return false;
        end
        local const_ui_str = U8toCUIStr(url);
        local result = qbweb_ptr:Navigate(const_ui_str);
         wegame_mainframe_view.inner_log(string.format(
            "navigateUrl, result:%s", tostring(result)), true);
        return result;
    end;

    set_list_panel_visible = function (is_show)
        local dialog_ptr = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.first_page_list_panel_id);
        if nil ~= dialog_ptr then
            if is_show then
               dialog_ptr:Show(TRUE);
            else
               dialog_ptr:Show(FALSE);
            end
        end
    end;

--    handle_list_panel_change = function ()
--        if nil == wegame_mainframe_view.dialog_ptr or nil == wegame_mainframe_view.first_page_list_panel_ptr then
--            return;
--        end

--        local list_panel_rc = wegame_mainframe_view.first_page_list_panel_ptr:GetRectEx(FALSE,TRUE);
--        local main_dialog_rc = wegame_mainframe_view.dialog_ptr:GetRectEx(TRUE,TRUE);
--        local main_dialog_w = main_dialog_rc.right - main_dialog_rc.left;
--        local main_dialog_h = main_dialog_rc.bottom - main_dialog_rc.top;

--        list_panel_rc.left = 1;
--        list_panel_rc.right = list_panel_rc.left + wegame_mainframe_model.game_list_width;
--        list_panel_rc.top = wegame_mainframe_model.top_panel_height - 1;
--        list_panel_rc.bottom = list_panel_rc.top + main_dialog_h - wegame_mainframe_model.top_panel_height;
--        wegame_mainframe_view.first_page_list_panel_ptr:MoveEx(list_panel_rc,TRUE);
--    end;

    save_jump_url = function (game_id, url)
        -- fixme mainframe_view尚未初始化
        if nil == web_operation then return end;
        web_operation.save_url_info(game_id, url);
    end;

    -- 跳转到指定的游戏页面
    jump_module_web = function (module_id, module_url)
        wegame_mainframe_view.inner_log(string.format("[jump_module_web]module_id:%d", module_id));
        local param = {};
        param.module_id = module_id;
        if nil ~= module_url and "" ~= module_url then
            param.module_url = module_url;
        end
        event_center.send_event(banner_event.JUMP_MODULE_WEB,
            base_utility.table_to_json(param));
    end;

    resize_wnd = function (width, height)
        wegame_mainframe_view.inner_log(string.format("[resize_wnd]width:%d,height:%d",
            width, height));
        ui_utility.control_resize_by_id(wegame_mainframe_view.dialog_id, width, height);
        wegame_mainframe_view.bring_mainframe_to_top();
    end;

    on_display_change = function ()
        wegame_mainframe_view.inner_log("get display change msg");
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil ~= tpfui then
            tpfui:RemoveInterval("wegame_mainframe_view.handle_display_change");
            tpfui:AddInterval("wegame_mainframe_view.handle_display_change", 2000);
        end
    end;

    handle_display_change = function ()
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil ~= tpfui then
            tpfui:RemoveInterval("wegame_mainframe_view.handle_display_change");
        end

        wegame_mainframe_view.inner_log("[on_display_change]");
        if wegame_mainframe_view.framestate == wegame_mainframe_view.e_framestate.min 
            or true == wegame_mainframe_view.is_closed then
            wegame_mainframe_model.unvisible_display_change = true;
            return;
        end

        local screan_width, screen_height = ui_utility.get_screen_resolution();
        if screan_width == wegame_mainframe_model.screan_width and 
            screen_height == wegame_mainframe_model.screan_height then
            return;
        end
        wegame_mainframe_model.screan_width = screan_width;
        wegame_mainframe_model.screan_height = screen_height;

        wegame_mainframe_view.adjust_mainframe_size(false);
    end;

    -- 判断窗口是否在屏幕可见范围之内: true表示可见，false表示不可见
    is_main_wnd_in_screen = function()
        local component = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.dialog_id);
        if component == nil then
            return false;
        end
        local rc = component:GetRectEx(FALSE,TRUE);
        local width = rc.right - rc.left;
        local height = rc.bottom - rc.top;
         -- 获取当前屏幕分辨率
        if 0 == wegame_mainframe_model.work_area_width or 0 == wegame_mainframe_model.work_area_height then
            local tpf_ui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            local sys_param = ierd_tgp.tpf_ui.SysParams:new();
            if nil ~= tpf_ui then
                tpf_ui:get_sys_params(sys_param);
                wegame_mainframe_model.work_area_width = sys_param.work_area_width;
                wegame_mainframe_model.work_area_height = sys_param.work_area_height;
                sys_param:delete();
            end
        end

        if rc.left + width < 0 or rc.left >= wegame_mainframe_model.work_area_width or
            rc.bottom + 10 < 0 or rc.top >= wegame_mainframe_model.work_area_height then
            return false;
        else
            return true;
        end  
    end;

    -- 初始化web框架
    init_web_frame = function()
        if true == wegame_mainframe_model.is_need_init_frame then
            wegame_mainframe_view.inner_log("init web_frame");
            local width, height = ui_utility.get_control_size_by_path(wegame_mainframe_view.dialog_id, 
                "pnlClient\\Ctn_WebArea");
            ui_utility.qbweb_setsize_by_path(wegame_mainframe_view.dialog_id,
                "pnlClient\\Ctn_WebArea\\QBWebBrowser", width, height);

            if true == wegame_mainframe_model.is_need_init_frame then
                wegame_mainframe_model.is_need_init_frame =
                not wegame_mainframe_view.navigate_url("", wegame_mainframe_model.frame_url);
            end
        end
    end;

    -- 加载web组件
    load_web_component = function()
        local web_component_ptr = ui_utility.get_ctrl_ptr_by_path(wegame_mainframe_view.dialog_id, 
        "pnlClient\\Ctn_WebArea");
        if web_component_ptr ~= nil then
            local is_load = web_component_ptr:IsLoad();
            if is_load ~= TRUE then
                wegame_mainframe_model.is_need_init_frame = true;
                local result = web_component_ptr:LoadContent();
                if result ~= TRUE then
                    -- 加载web组件失败，800ms之后重试
                    local tpfui =
                        base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");
                    tpfui:AddInterval('wegame_mainframe_view.delay_load_web_timer', 800);
                end
            end
            wegame_mainframe_view.init_web_frame();
        end
    end;

    -- 卸载web组件
    unload_web_component = function()
        local main_id = ui_utility.get_ctrl_id_by_cid(CID_MAINFRAME_Dialog_MainFrame);
        local web_component_ptr = ui_utility.get_ctrl_ptr_by_path(main_id,"pnlClient\\Ctn_WebArea");
        if web_component_ptr ~= nil then
            web_component_ptr:UnLoadContent();
        end
        -- 需要重新初始化框架
        wegame_mainframe_view.inner_log("unload_web_component");
        wegame_mainframe_model.is_need_init_frame = true;
        wegame_mainframe_model.web_mainframe_ready = false;
    end;

    -- 延迟加载web组件
    delay_load_web_timer = function()
        wegame_mainframe_view.inner_log("due to load failer, again load_web_component.");
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval('wegame_mainframe_view.delay_load_web_timer');
        local web_component_ptr = ui_utility.get_ctrl_ptr_by_path(wegame_mainframe_view.dialog_id, 
                "pnlClient\\Ctn_WebArea");
        if web_component_ptr ~= nil then
            local is_load = web_component_ptr:IsLoad();
            if is_load ~= TRUE then
                wegame_mainframe_model.is_need_init_frame = true;
                web_component_ptr:LoadContent()
            end
            wegame_mainframe_view.init_web_frame();
        end
    end;

    -- 预加载布局文件
    restore_layout = function()
        wegame_mainframe_view.inner_log("restore_layout wegame_mainframe.tpf");
        local dialog_id = 0;
        if Tenio.INVALID_CONTROLID == wegame_mainframe_view.dialog_id then
            dialog_id = layout_mgr.restore_layout("wegame_mainframe\\wegame_mainframe.tpf");
        end

        -- load_web_component
        local web_component_ptr = ui_utility.get_ctrl_ptr_by_path(dialog_id, 
        "pnlClient\\Ctn_WebArea");
        if web_component_ptr ~= nil then
            local is_load = web_component_ptr:IsLoad();
            if is_load ~= TRUE then
                local result = web_component_ptr:LoadContent();
                if result ~= TRUE then
                    wegame_mainframe_view.inner_log("restore_layout wegame_mainframe.tpf fail");
                end
            end

            wegame_mainframe_view.inner_log("init web_frame");
            local width, height = ui_utility.get_control_size_by_path(dialog_id, 
                "pnlClient\\Ctn_WebArea");
            ui_utility.qbweb_setsize_by_path(dialog_id,
                "pnlClient\\Ctn_WebArea\\QBWebBrowser", width, height);

            -- 加载url
            local url = LoadStr("WEGAME_FRAME_INDEX_URL");
            if url == nil then
                wegame_mainframe_view.inner_log("navigateUrl：url is nil");
                return false;
            end

            wegame_mainframe_view.inner_log("navigateUrl："..url);
            wegame_mainframe_view.current_url = url;
            local webctn_ptr = ui_utility.get_ctrl_ptr_by_path(dialog_id,
                "pnlClient\\Ctn_WebArea\\QBWebBrowser");

            if webctn_ptr == nil then
                wegame_mainframe_view.inner_log("navigateUrl, webctn_ptr is nil");
                return false;
            end

            local qbweb_ptr = tolua.cast(webctn_ptr, "Tenio::IQBWebBrowser");
            if qbweb_ptr == nil then
                wegame_mainframe_view.inner_log("navigateUrl, qbweb_ptr is nil");
                return false;
            end
            local const_ui_str = U8toCUIStr(url);
            local result = true;
            if true == wegame_mainframe_model.is_need_init_frame then
                result = qbweb_ptr:Navigate(const_ui_str);
                wegame_mainframe_view.inner_log(string.format(
                "restore_layout, navigateUrl, result:%s", tostring(result)), true);
                wegame_mainframe_model.is_need_init_frame = not result;
            end
            return result;
        end
    end;

    -- 检测是否接管消息
    check_ime_msg_cfg = function()
        local dwQBlink = ui_utility.get_ctrl_id_by_path(wegame_mainframe_view.dialog_id,
                            "pnlClient\\Ctn_WebArea\\QBWebBrowser");

        if nil == dwQBlink or 0 == dwQBlink then
            wegame_mainframe_view.inner_log("check WM_IME_CHAR message, webctn_ptr is nil");
            return;
        end;

        local   zTemp = {};

        zTemp["key"] = "takeover_ime_msg";

        local   nData = comm_center.call_service("i_svr_get_common_cfg", zTemp);

        if nil ~= nData and nData.value ~= nil then
            ui_utility.setproperty_by_id(dwQBlink, "takeover_ime_msg", tostring(nData.value));
        elseif nil ~= zTemp["value"] then
            ui_utility.setproperty_by_id(dwQBlink, "takeover_ime_msg", tostring(zTemp["value"]));
        else
            wegame_mainframe_view.inner_log("Get QBlink takeover_ime_msg message fail.");
        end;
    end;

    is_hidden = function()
        local is_hide = 
            wegame_mainframe_view.framestate == wegame_mainframe_view.e_framestate.min or 
                wegame_mainframe_view.is_closed;
        wegame_mainframe_view.inner_log(string.format("[is_hidden]framestate:%s,is_closed:%s",
        tostring(wegame_mainframe_view.framestate), tostring(wegame_mainframe_view.is_closed)));
        return is_hide;
    end; 

    -- 设置client区背景颜色
    set_module_color = function (module_color)
        if nil == module_color then
            return;
        end
        wegame_mainframe_view.inner_log(string.format("[set_module_color]module_color:%s", module_color));
        ui_utility.setproperty_by_id(wegame_mainframe_view.dialog_id, "Background", module_color);
    end;

    set_login_srv_state_mark = function()
        if wegame_mainframe_model.login_srv_state then
            -- ui_utility.setproperty_by_cid(CID_MAIN_TOP_LOGIN_FAIL_MARK, "Visible", "false");
        else
            -- ui_utility.setproperty_by_cid(CID_MAIN_TOP_LOGIN_FAIL_MARK, "Visible", "true");
        end;
    end;

    pack_up_game_list = function (is_pack_up)
        wegame_mainframe_view.inner_log(string.format("[pack_up_game_list]is_pack_up:%s", tostring(is_pack_up)), true);
        wegame_mainframe_model.is_game_list_pack_up = is_pack_up;
        if true == wegame_mainframe_model.is_game_list_pack_up then
            wegame_mainframe_view.set_list_panel_visible(false);
            wegame_mainframe_view.set_pack_up_btn_visible(true);
        else
            wegame_mainframe_view.set_list_panel_visible(true);
        end
        if true == wegame_mainframe_view.is_pack_up_btn_visible() then
            wegame_mainframe_view.adjust_pack_up_btn_position();
        end
    end;

    adjust_pack_up_btn_position = function ()
         wegame_mainframe_view.inner_log("adjust_pack_up_btn_position");
        local dialog_ptr = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.dialog_id);
        local list_ptr = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.first_page_list_panel_id);
        local btn_ptr = ui_utility.get_ctrl_ptr_by_path(wegame_mainframe_view.dialog_id, "PackUpCtn");

        if nil == dialog_ptr or nil == list_ptr or nil == btn_ptr then
            return;
        end
        -- btn_ptr:Show(TRUE);
        local mainframe_rc = dialog_ptr:GetRectEx(TRUE,TRUE);
        local list_rc = list_ptr:GetRectEx(TRUE,TRUE);
        local btn_rc = btn_ptr:GetRectEx(TRUE,TRUE);
        local btn_h = btn_rc.bottom - btn_rc.top;
        local btn_w = btn_rc.right - btn_rc.left;
        local list_w = list_rc.right - list_rc.left;

        local mainframe_h = mainframe_rc.bottom - mainframe_rc.top;

        if true == wegame_mainframe_model.is_game_list_pack_up then
            btn_rc.top = (mainframe_rc.top + 80 + (mainframe_h - 80) / 2 - btn_h / 2) - mainframe_rc.top;
            btn_rc.bottom = btn_rc.top + btn_h;
            btn_rc.left = 0;
            btn_rc.right = btn_rc.left + btn_w;
            btn_ptr:MoveEx(btn_rc,TRUE);
        else
            btn_rc.top = (mainframe_rc.top + 80 + (mainframe_h - 80) / 2 - btn_h / 2) - mainframe_rc.top;
            btn_rc.bottom = btn_rc.top + btn_h;
            btn_rc.left = list_w + 1;
            btn_rc.right = btn_rc.left + btn_w;
            btn_ptr:MoveEx(btn_rc,TRUE);
        end
    end;

    set_pack_up_btn_visible = function (is_show)
        local btn_ptr = ui_utility.get_ctrl_ptr_by_path(wegame_mainframe_view.dialog_id, "PackUpCtn");

        if nil == btn_ptr then
            return;
        end
        
        if true == is_show then
            btn_ptr:Show(TRUE);
        else
            btn_ptr:Show(FALSE);
        end
          -- btn_ptr:Show(TRUE);
    end;

    on_pack_up_btn_click = function (uilib, control_id)
        -- 先隐藏按钮和游戏列表
        wegame_mainframe_view.set_pack_up_btn_visible(false);
        wegame_mainframe_view.set_list_panel_visible(false);
        -- 调用接口进行页面向左移动动画
        local move_type = 1;
        if true == wegame_mainframe_model.is_game_list_pack_up then
            move_type = 1; -- 展开动画
            base_utility.mainframe_report("main_window", "click_pack_up", "1");
        else
            move_type = 2; -- 收起动画
            base_utility.mainframe_report("main_window", "click_pack_up", "2");
        end
        wegame_mainframe_view.web_move_animation(move_type);
        wegame_mainframe_view.adjust_helper_bottom_bar_size_before_ani();
    end;

    pack_up_list_animation_end = function (move_type)
        -- 收到动画结束的通知
        base_utility.remove_interval("wegame_mainframe_view.handle_pack_up_list_animation_end", tonumber(move_type));
        if 1 == tonumber(move_type) then
            wegame_mainframe_model.is_game_list_pack_up = false;
            wegame_mainframe_view.set_list_panel_visible(true);
        else
            wegame_mainframe_model.is_game_list_pack_up = true;
        end
        wegame_mainframe_view.set_pack_up_btn_visible(true);
        wegame_mainframe_view.adjust_pack_up_btn_position();
        wegame_mainframe_view.set_pack_up_btn_bkg_core(wegame_mainframe_model.cur_pack_up_btn_color_flag);
    end;

    adjust_helper_bottom_bar_size = function ()
        -- 助手底栏
        local empty_id = ui_utility.get_ctrl_id_by_path(wegame_mainframe_view.dialog_id, "pnlClient\\Ctn_BottomBar\\Ctn_Empty");
        if false == wegame_mainframe_model.is_game_list_pack_up then
            ui_utility.setproperty_by_id(empty_id, "Visible", "true");
        else
            ui_utility.setproperty_by_id(empty_id, "Visible", "false");
        end
    end;

    adjust_helper_bottom_bar_size_before_ani = function ()
        local empty_id = ui_utility.get_ctrl_id_by_path(wegame_mainframe_view.dialog_id, "pnlClient\\Ctn_BottomBar\\Ctn_Empty");
        if true == wegame_mainframe_model.is_game_list_pack_up then
            ui_utility.setproperty_by_id(empty_id, "Visible", "true");
        else
            ui_utility.setproperty_by_id(empty_id, "Visible", "false");
        end
    end;

    set_pack_up_btn_backgroup = function (game_id)
        local data = {game_id = game_id};
        local color_info = comm_center.call_service("SrvGetListColorFlag", data);
        if nil == color_info or nil == color_info.color_flag then
            return;
        end
        wegame_mainframe_model.cur_pack_up_btn_color_flag = color_info.color_flag;
        base_utility.log(string.format("[set_pack_up_btn_backgroup]game_id:%d, color:%d",
            game_id, wegame_mainframe_model.cur_pack_up_btn_color_flag));
        wegame_mainframe_view.set_pack_up_btn_bkg_core(wegame_mainframe_model.cur_pack_up_btn_color_flag);
    end;

    set_pack_up_btn_bkg_core = function (flag)
        local btn_id = ui_utility.get_ctrl_id_by_path(wegame_mainframe_view.dialog_id, "PackUpCtn\\BtnPackUp");
        if 1 == flag then -- 深色
            if true == wegame_mainframe_model.is_game_list_pack_up then -- 收起
                ui_utility.setproperty_by_id(btn_id, "Background", "ONEIMAGE:client_mainframe\\pack_up_btn.png?40,97,52,177;Stretch;0;");
                ui_utility.setproperty_by_id(btn_id, "BackgroundHover", "ONEIMAGE:client_mainframe\\pack_up_btn.png?57,97,69,177;Stretch;0;");
                ui_utility.setproperty_by_id(btn_id, "BackgroundPressed", "ONEIMAGE:client_mainframe\\pack_up_btn.png?40,97,52,177;Stretch;0;");
            else  -- 展开
                ui_utility.setproperty_by_id(btn_id, "Background", "ONEIMAGE:client_mainframe\\pack_up_btn.png?6,97,18,177;Stretch;0;");
                ui_utility.setproperty_by_id(btn_id, "BackgroundHover", "ONEIMAGE:client_mainframe\\pack_up_btn.png?23,97,35,177;Stretch;0;");
                ui_utility.setproperty_by_id(btn_id, "BackgroundPressed", "ONEIMAGE:client_mainframe\\pack_up_btn.png?6,97,18,177;Stretch;0;");
            end
        else -- 浅色
            if true == wegame_mainframe_model.is_game_list_pack_up then  -- 收起
                ui_utility.setproperty_by_id(btn_id, "Background", "ONEIMAGE:client_mainframe\\pack_up_btn.png?40,10,52,90;Stretch;0;");
                ui_utility.setproperty_by_id(btn_id, "BackgroundHover", "ONEIMAGE:client_mainframe\\pack_up_btn.png?57,10,69,90;Stretch;0;");
                ui_utility.setproperty_by_id(btn_id, "BackgroundPressed", "ONEIMAGE:client_mainframe\\pack_up_btn.png?40,10,52,90;Stretch;0;");
            else  -- 展开
                ui_utility.setproperty_by_id(btn_id, "Background", "ONEIMAGE:client_mainframe\\pack_up_btn.png?6,10,18,90;Stretch;0;");
                ui_utility.setproperty_by_id(btn_id, "BackgroundHover", "ONEIMAGE:client_mainframe\\pack_up_btn.png?23,10,35,90;Stretch;0;");
                ui_utility.setproperty_by_id(btn_id, "BackgroundPressed", "ONEIMAGE:client_mainframe\\pack_up_btn.png?6,10,18,90;Stretch;0;");
            end
        end
    end;

    on_list_dlg_hover = function (uilib, control_id)
        wegame_mainframe_view.inner_log("[on_list_dlg_hover]");
        wegame_mainframe_view.set_pack_up_btn_visible(true);
        wegame_mainframe_view.adjust_pack_up_btn_position();
        if nil ~= banner_view then
            local module_id = banner_view.get_current_select_module();
            if module_id ~= nil then
                wegame_mainframe_view.set_pack_up_btn_backgroup(module_id);
            end;
        end
    end;

    on_list_dlg_leave = function (uilib, control_id)
        wegame_mainframe_view.inner_log("[on_list_dlg_leave]");
        local tpfui = base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil ~= tpfui then
            tpfui:RemoveInterval("wegame_mainframe_view.delay_handle_list_leave_msg");
            tpfui:AddInterval("wegame_mainframe_view.delay_handle_list_leave_msg", 1000);
            base_utility.log("[wegame_mainframe_view]on_list_dlg_leave");
        end
    end;

    delay_handle_list_leave_msg = function ()
        local tpfui = base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval('wegame_mainframe_view.delay_handle_list_leave_msg');
        local mouse_in = wegame_mainframe_view.is_mouse_in_list_and_btn();
        if not mouse_in then
            local pack_up_btn_ptr = ui_utility.get_ctrl_ptr_by_path(wegame_mainframe_view.dialog_id, "PackUpCtn");
            local is_show = FALSE;
            if pack_up_btn_ptr ~= nil then
                is_show = pack_up_btn_ptr:IsVisible();
            end
            local list_visible = FALSE;
            local dialog_ptr = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.first_page_list_panel_id);
            if nil ~= dialog_ptr then
                list_visible = dialog_ptr:IsVisible();
            end
            if is_show == TRUE and TRUE == list_visible then
                wegame_mainframe_view.set_pack_up_btn_visible(false);
            end 
        end
    end;

    is_pack_up_btn_visible = function ()
        local pack_up_btn_ptr = ui_utility.get_ctrl_ptr_by_path(wegame_mainframe_view.dialog_id, "PackUpCtn");
        if nil == pack_up_btn_ptr then
            return false;
        end
        local is_visible = false;
        if TRUE == pack_up_btn_ptr:IsVisible() then
            is_visible = true;
        end
        return is_visible;
    end;

    is_mouse_in_list_and_btn = function ()
        local list_ptr = ui_utility.get_ctrl_ptr_by_path(wegame_mainframe_view.dialog_id, "DlgList");
        local btn_ptr = ui_utility.get_ctrl_ptr_by_path(wegame_mainframe_view.dialog_id, "PackUpCtn\\BtnPackUp");

        if nil == list_ptr or nil == btn_ptr then
            return false;
        end

        local rect_list = list_ptr:GetRect(TRUE); 
        local point = ierd_tgp.tray_ui.Point:new_local();
        local tray_ui =
            base_utility.get_component_adapter("ITray_ui",
                "ierd_tgp.tray_ui.get_Tray_ui_adapter();");
        if tray_ui ~= nil then
            tray_ui:get_cursor_pos(point);
        end             
        local mouse_in = false;
        if rect_list.left <= point.x and point.x <= rect_list.right and
            rect_list.top <= point.y and point.y <= rect_list.bottom then
            mouse_in = true;
        end

        if not mouse_in then
            -- 判断是否在btn里
            local rect_btn = btn_ptr:GetRect(TRUE);
            if rect_btn.left <= point.x and point.x <= rect_btn.right and
                rect_btn.top <= point.y and point.y <= rect_btn.bottom then
                mouse_in = true;
            end
        end
        return mouse_in;
    end;

    is_main_wnd_postion_or_size_abnormal = function ()
        if nil == wegame_mainframe_view.dialog_ptr then
            return false;
        end

        local cur_resolution_x = 0;
        local cur_resolution_y = 0;
        local cur_work_area_x = 0;
        local cur_work_area_y = 0;
        cur_resolution_x, cur_resolution_y, cur_work_area_x, cur_work_area_y = ui_utility.get_screen_resolution();
        local mainframe_rc = wegame_mainframe_view.dialog_ptr:GetRectEx(TRUE,TRUE);
        local mainframe_w = mainframe_rc.right - mainframe_rc.left;
        local mainframe_h = mainframe_rc.bottom - mainframe_rc.top;

        -- 不可见或者尺寸小于最小尺寸都认为是异常
        if mainframe_rc.right < 0 or mainframe_rc.left > cur_resolution_x 
            or mainframe_rc.bottom < 0 or mainframe_rc.top > cur_resolution_y
            or mainframe_w < wegame_mainframe_model.min_wnd_width  
            or mainframe_h < wegame_mainframe_model.min_wnd_height then
            return true;
        end
    end;

    web_move_animation = function (move_type)
        -- 调用接口进行页面向左移动动画
        local callback_func = web_register_center.get_func_by_browser(
            "web_mainframe_comm_listen", CID_MAINFRAME_QBWEB);
        if callback_func ~= nil and callback_func ~= "" then
            local move_info = {};
            move_info["move_type"] = move_type;
            local rsp_data_table = 
                web_utility.notify_event_comm_data("web_mainframe_comm_listen");
            rsp_data_table.data.data = move_info;
            rsp_data_table.data.custom_cmd = "web_move_animation";
            web_utility.lua_call_js_ex(callback_func, 
                base_utility.table_to_json(rsp_data_table), CID_MAINFRAME_QBWEB);
        end
    end;

    bring_mainframe_to_top = function (args)
        if nil ~= wegame_mainframe_view.dialog_ptr then
            wegame_mainframe_view.dialog_ptr:BringToTop();
            wegame_mainframe_view.dialog_ptr:MoveToCenter();
        end
    end;

    set_title_operation_skin = function (is_show, img_url, bkg_color, drop_img)
        if nil == is_show then
            return;
        end
        
        if 1 == is_show then
            ui_utility.setproperty_by_id(wegame_mainframe_view.title_operation_id, "Visible", "true");
            if nil ~= img_url or "" ~= img_url then
                local url = "ONEIMAGE:client_mainframe\\top_title\\operation.png|" .. img_url .. ";Center;0;";   
                ui_utility.setproperty_by_id(wegame_mainframe_view.title_operation_id, "Background", url);
            end
            if nil ~= bkg_color or "" ~= bkg_color then
                local bkg_color_format = ui_utility.convert_hex_color_to_str(bkg_color);
                ui_utility.setproperty_by_id(wegame_mainframe_view.title_id, "Background", bkg_color_format);
            end
            if nil ~= drop_img or "" ~= drop_img then
                local url = "ONEIMAGE:client_mainframe\\top_title\\drap.png|" .. drop_img .. ";Stretch;0";
                ui_utility.setproperty_by_id(wegame_mainframe_view.drop_id, "Background", url);
            end
        else
            ui_utility.setproperty_by_id(wegame_mainframe_view.title_operation_id, "Visible", "false");
            ui_utility.setproperty_by_id(wegame_mainframe_view.title_id, "Background", "COLOR:RGB:48,48,51");
            local url = "ONEIMAGE:client_mainframe\\top_title\\drap.png;Stretch;0";
            ui_utility.setproperty_by_id(wegame_mainframe_view.drop_id, "Background", url);
        end
    end;

    -- 是否是全屏
    is_main_wnd_full_screen = function ()
        if nil == wegame_mainframe_view.dialog_ptr then
            return false;
        end
        local is_full_screen = false;
        local cur_resolution_x = 0;
        local cur_resolution_y = 0;
        local cur_work_area_x = 0;
        local cur_work_area_y = 0;
        cur_resolution_x, cur_resolution_y, cur_work_area_x, cur_work_area_y = ui_utility.get_screen_resolution();
        local mainframe_rc = wegame_mainframe_view.dialog_ptr:GetRectEx(TRUE,TRUE);
        local mainframe_w = mainframe_rc.right - mainframe_rc.left;
        local mainframe_h = mainframe_rc.bottom - mainframe_rc.top;

        if mainframe_w >= cur_work_area_x and mainframe_h >= cur_work_area_y then
            is_full_screen = true;
        end
        return is_full_screen;
    end;

    on_click_app_btn = function ()
        local tips_url = app_tips.tips_url or "";
        if "" ~= tips_url then
            local param = { 
                type = "open_web", 
                browser = "new", 
                use_ptlogin = 1, 
                url = tips_url
            };
            comm_center.call_service("i_service_open_link_confirm_launch", param);
            app_tips.report_operation(app_tips_actions.kActionClick);
        end
    end;

    on_enter_app_btn = function ()
        local data = {};
        data.is_show = true;
        comm_center.call_service("SvrShowAPPTips", data);
    end;

    on_leave_app_btn = function ()
        local data = {};
        data.is_show = false;
        comm_center.call_service("SvrShowAPPTips", data);
    end;

    request_user_login_time = function(tgp_id)
        local post_data = {
            tgpid = tgp_id,
            login_type = 2
        };
        local url = LoadStr("QUERY_USER_LOGIN_TIME_URL");
        base_utility.curl_request_async(url, "wegame_mainframe_view.on_request_user_login_time",
                    false, nil , true, nil, base_utility.table_to_json(post_data));
    end;

    on_request_user_login_time = function(rsp)
        
        wegame_mainframe_view.has_app_login = false;

        base_utility.log("[user_login_time] rsp: ".. rsp);

        local rsp_tbl = base_utility.json_to_table(rsp);

        if type(rsp_tbl) == 'table' and
            nil ~= rsp_tbl.data and
            nil ~= rsp_tbl.data.result then

            local result = rsp_tbl.data.result;
            if 0 == result or 200 == result then

                local seconds = 1;
                local minutes = 1 * 60 * seconds;
                local hours = 1 * 60 * minutes;
                local days = 1 * 24 * hours;

                -- unix timestamp
                local last_app_login_time = rsp_tbl.data.time or 0;
                local diff_time = os.difftime(os.time(), last_app_login_time);
                if diff_time < 7 * days then
                    wegame_mainframe_view.has_app_login = true;
                end

            end
        end

        if nil ~= wegame_mainframe_view.app_tips_data then
            wegame_mainframe_view.set_app_tips_config(wegame_mainframe_view.app_tips_data);
        end

    end;

    set_app_tips_config = function(data)

        if base_utility.CLIENT_TYPE.CHINA_VER ~= base_utility.get_client_version_type() then
            return;
        end

        if nil == data or nil == data.is_show or 
             nil == data.qrcode_img_new then
            return;
        end

        if 1 ~= tonumber(data.is_show) then
            ui_utility.setproperty_by_id(wegame_mainframe_view.app_pnl_id, "Visible", "false");
            return;
        end

        if nil == wegame_mainframe_view.has_app_login then --尚未获取到登录记录,缓存数据待后续设置
            wegame_mainframe_view.app_tips_data = data;
            wegame_mainframe_view.request_user_login_time(base_utility.get_tgp_id());
            return;
        end
        
        if wegame_mainframe_view.has_app_login then
            ui_utility.setproperty_by_id(wegame_mainframe_view.app_btn_id,
                "Icon", "client_mainframe\\top_title\\app_btn_hover.png");
            ui_utility.setproperty_by_id(wegame_mainframe_view.app_btn_id, 
                "TextColor", "RGB:255,200,9");
            ui_utility.setproperty_by_id(wegame_mainframe_view.app_btn_id, 
                "TextColorHover", "RGB:255,200,9");
            ui_utility.setproperty_by_id(wegame_mainframe_view.app_btn_id, 
                "TextColorPressed", "RGB:255,200,9");
        else
            ui_utility.setproperty_by_id(wegame_mainframe_view.app_btn_id,
                "Icon", "client_mainframe\\top_title\\app_btn_nor.png");
            ui_utility.setproperty_by_id(wegame_mainframe_view.app_btn_id, 
                "TextColor", "RGB:164,163,164");
            ui_utility.setproperty_by_id(wegame_mainframe_view.app_btn_id, 
                "TextColorHover", "RGB:164,163,164");
            ui_utility.setproperty_by_id(wegame_mainframe_view.app_btn_id, 
                "TextColorPressed", "RGB:164,163,164");
        end

        ui_utility.setproperty_by_id(wegame_mainframe_view.app_pnl_id, "Visible", "true");

        if "" ~= data.qrcode_img_new then
           app_tips.set_background_image(data.qrcode_img_new);
        end

        if nil ~= data.tips_url then
           app_tips.tips_url = data.tips_url;
        end

        -- 最近未使用app登录且开启开关则弹出推广
        if 1 == tonumber(data.tips_show_type) and
            not wegame_mainframe_view.has_app_login then
            local param = { is_show = true };
            comm_center.call_service("SvrShowAPPTips", param);
            local app_btn_ptr = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.app_btn_id);
            if nil ~= app_btn_ptr then -- fixme 当前通过焦点变化隐藏app tips，依赖焦点正常在该按钮上。
               app_btn_ptr:SetFocus();
            end
        end
        
        -- qos data
        if wegame_mainframe_view.has_app_login then
            app_tips.report_operation(app_tips_actions.kActionLight);
        else
            app_tips.report_operation(app_tips_actions.kActionUnlight);
        end
    end;

    move_list = function ()
        local dialog_ptr = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.dialog_id);
        local list_ptr = ui_utility.get_ctrl_ptr_by_id(wegame_mainframe_view.first_page_list_panel_id);

        if nil ~= dialog_ptr and nil ~= list_ptr then
            local dialog_rect = dialog_ptr:GetRectEx(FALSE, TRUE);
            local list_rect = list_ptr:GetRectEx(TRUE, TRUE);
            local list_height = list_rect.bottom - list_rect.top;
            local list_width = list_rect.right - list_rect.left;
            list_rect.top = dialog_rect.top + 80;
            list_rect.bottom = list_rect.top + list_height;
            list_rect.left = dialog_rect.left + 1;
            list_rect.right = list_rect.left + list_width;

            list_ptr:MoveEx(list_rect, TRUE);
        end
    end;

    on_load_page_complete = function ()
        if nil == wegame_mainframe_view.dialog_ptr then
            return;
        end
        if wegame_mainframe_view.is_mouse_in_list_and_btn() then
            wegame_mainframe_view.dialog_ptr:SetFocus();
        end
    end;

    on_full_screen_change = function (uilib, mode_type, control_id)
        if nil == wegame_mainframe_view.first_page_list_panel_ptr or
            nil == wegame_mainframe_view.pack_up_btn_ptr then
            return;
        end

        if wegame_mainframe_view.FULLSCREEN_MODE_TYPE.FULLSCREEN_MODE == mode_type then
            if TRUE == wegame_mainframe_view.first_page_list_panel_ptr:IsVisible() then
                wegame_mainframe_view.is_list_screen_mode_visible = true;
                wegame_mainframe_view.first_page_list_panel_ptr:Show(FALSE);
                
            end

            if TRUE == wegame_mainframe_view.pack_up_btn_ptr:IsVisible() then
                wegame_mainframe_view.is_packup_btn_screen_mode_visible = true;
                wegame_mainframe_view.pack_up_btn_ptr:Show(FALSE);
            end
        else
            if true == wegame_mainframe_view.is_list_screen_mode_visible  then
                wegame_mainframe_view.first_page_list_panel_ptr:Show(TRUE);
                wegame_mainframe_view.is_list_screen_mode_visible = false;
            end
            if true == wegame_mainframe_view.is_packup_btn_screen_mode_visible then
                wegame_mainframe_view.pack_up_btn_ptr:Show(TRUE);
                wegame_mainframe_view.is_packup_btn_screen_mode_visible = false;
            end
        end
    end;

    set_browser_working_set_size = function(min_size, max_size)
        if min_size == nil or max_size == nil or
           tonumber(min_size) == nil or tonumber(max_size) == nil then
            return;
        end;

        local web_ptr = ui_utility.get_ctrl_ptr_by_path(wegame_mainframe_view.dialog_id,
                                                           "pnlClient\\Ctn_WebArea\\QBWebBrowser");
        if web_ptr == nil then
            return;
        end
        local qbweb_ptr = tolua.cast(web_ptr, "Tenio::IQBlinkWebBrowser");
        if qbweb_ptr == nil then
            return;
        end
        qbweb_ptr:SetWorkingSetSize(min_size, max_size);
    end;

    query_is_mainframe_iconic = function()
       local is_iconic = false;
       if wegame_mainframe_view.dialog_ptr ~= nil then
           local hwnd = wegame_mainframe_view.dialog_ptr:GetHwnd();
           local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
           if hwnd == nil then
               wegame_mainframe_view.inner_log("[show]query_is_mainframe_iconic hwnd nil");
           end
           
           if tpfui == nil then
              wegame_mainframe_view.inner_log("[show]query_is_mainframe_iconic tpfui nil");
           end
           
           if hwnd ~= nil and tpfui ~= nil then
              if tpfui:IsIconic(hwnd) then
                 is_iconic = true;
              end;
           end;
       end;
       local param = {};
       param.is_iconic = is_iconic;
       if component ~= nil and component:IsVisible() ~= TRUE then
           param.is_iconic = true;
       end
       wegame_mainframe_view.inner_log("[show]query_is_mainframe_iconic ".. tostring(param.is_iconic));
      comm_center.call_service("i_service_is_mainframe_iconic_result", param);
    end;

    should_show_red_dot = function(key_name)
        local info = {common = 0, key = key_name};
        local config = comm_center.call_service("Svr_GetConfig", info);
        if nil ~= config and 1 == config.exits then
            local cfg_tbl = base_utility.json_to_table(config.value or "{}");
            if config.value == 1 or config.value == "1" then
                return false;
            end
        end
        return true;
    end;
};
